﻿using System;
using System.Collections.Generic;
using System.Text;
using GroupChat2013.Common;
using GroupChat2013.Interface;
using GroupChat2013.Logic;
using GroupChat2013.Logic.Interface;
using GroupChat2013.Plugins;
using GroupChat2013.Main.Properties;
using GroupChat2013.Entity;

namespace GroupChat2013.Main.Components
{
    /// <summary>
    /// 获取未读消息处理
    /// </summary>
    public class UnReadMesageProcess : IReceiveProcess
    {
        private FrmMain frmMain;

        private FrmInformation frmInformation;

        /// <summary>
        /// 消息处理标志
        /// </summary>
        private string key;

        private string hasUnreadMessageType = "hasUnreadMessageType";

        /// <summary>
        /// 构造函数
        /// </summary>
        public UnReadMesageProcess(FrmMain frmMain, FrmInformation frmInformation)
        {
            this.frmMain = frmMain;
            this.frmInformation = frmInformation;

            key = ConstClass.GETHASUNREADMISSINGMESSAGE;
        }

        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool Process(Entity.Protocol.TransferMsgInfo message)
        {
            if (!message.action.Equals(key))
                return false;

            if (message.data.ContainsKey(hasUnreadMessageType))
            {
                string hasUnreadMessage = message.data[hasUnreadMessageType];

                if (hasUnreadMessage == "1")
                {
                    frmMain.SetHasReadMsg();

                    ButtonConfigInfo info = frmMain.GetHasReadButtonData();

                    InformationItem item = new InformationItem()
                    {
                        Url = info.link,
                        Content = "您有新的离线消息，请点击查看",
                        CallBack = () => {
                            frmMain.SetUnReadMsg();
                        }
                    };

                    if (frmInformation == null || frmInformation.IsDisposed)
                        frmInformation = new FrmInformation();

                    frmInformation.Show();
                    frmInformation.AddInformationItem(item);
                }

                return true;
            }

            return false;
        }
    }
}
